package com.guice.web.guice_webapp_example.mybatis;

import com.google.inject.name.Names;
import com.guice.web.guice_webapp_example.mybatis.mapper.UserMapper;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.guice.MyBatisModule;
import org.mybatis.guice.datasource.builtin.PooledDataSourceProvider;
import org.mybatis.guice.datasource.helper.JdbcHelper;

import java.util.Properties;

public class MybatisModule extends MyBatisModule {
    @Override
    protected void initialize() {
        install(JdbcHelper.MySQL);

        bindDataSourceProviderType(PooledDataSourceProvider.class);
        bindTransactionFactoryType(JdbcTransactionFactory.class);
        addMapperClass(UserMapper.class);
        Names.bindProperties(binder(), createTestProperties());
    }

    public Properties createTestProperties() {
        Properties myBatisProperties = new Properties();
        myBatisProperties.setProperty("mybatis.environment.id", "test");
        myBatisProperties.setProperty("JDBC.schema", "mybatis-guice_TEST");
        myBatisProperties.setProperty("derby.create", "true");
        myBatisProperties.setProperty("JDBC.username", "root");
        myBatisProperties.setProperty("JDBC.password", "123456");
        myBatisProperties.setProperty("JDBC.autoCommit", "true");
        return myBatisProperties;
//        Names.bindProperties(binder, myBatisProperties);
    }
}
